package com.vegetable.modules.controller.finance;

import com.vegetable.common.utils.R;
import com.vegetable.modules.finance.service.ArrearsService;
import com.vegetable.modules.vo.finance.ArrearsStatisticsVO;
import com.vegetable.modules.vo.finance.ArrearsDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
// import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.Map;

/**
 * 欠款统计控制器
 */
@Slf4j
@RestController
@RequestMapping("/finance/arrears")
@Api(tags = "欠款统计")
public class ArrearsController {
    
    @Autowired
    private ArrearsService arrearsService;
    
    /**
     * 计算公司总欠款详情
     */
    @GetMapping("/statistics")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算公司总欠款详情", notes = "获取公司总欠款详情，包含代办欠款、采购欠款及其细分统计")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R getArrearsStatistics(
            @ApiParam(value = "代办人ID", required = false) @RequestParam(required = false) Long agentId,
            @ApiParam(value = "客户ID", required = false) @RequestParam(required = false) Long customerId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        ArrearsStatisticsVO vo = arrearsService.getArrearsStatistics(agentId, customerId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("arrearsStatistics", vo);
        return R.data(result);
    }
    
    /**
     * 批量获取多个代办人的欠款统计
     */
    @GetMapping("/agent/list")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "批量获取多个代办人的欠款统计", notes = "根据代办人ID列表批量获取欠款统计信息")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R getAgentArrearsList(
            @ApiParam(value = "代办人ID列表，逗号分隔", required = true) @RequestParam String agentIds,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        List<Long> agentIdList = Arrays.stream(agentIds.split(","))
                .map(String::trim)
                .map(Long::valueOf)
                .collect(Collectors.toList());
        
        List<ArrearsStatisticsVO> result = arrearsService.getAgentArrearsList(agentIdList, regionId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("agentArrearsList", result);
        return R.data(resultMap);
    }
    
    /**
     * 批量获取多个客户的欠款统计
     */
    @GetMapping("/customer/list")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "批量获取多个客户的欠款统计", notes = "根据客户ID列表批量获取欠款统计信息")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R getCustomerArrearsList(
            @ApiParam(value = "客户ID列表，逗号分隔", required = true) @RequestParam String customerIds,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        List<Long> customerIdList = Arrays.stream(customerIds.split(","))
                .map(String::trim)
                .map(Long::valueOf)
                .collect(Collectors.toList());
        
        List<ArrearsStatisticsVO> result = arrearsService.getCustomerArrearsList(customerIdList, regionId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("customerArrearsList", result);
        return R.data(resultMap);
    }
    
    /**
     * 获取欠款明细列表
     */
    @GetMapping("/detail/list")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "获取欠款明细列表", notes = "获取代办和采购的欠款明细列表，包含类型、名称、交易金额、未对账金额、未支付金额、欠款金额、台账等信息")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R getArrearsDetailList(
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        List<ArrearsDetailVO> result = arrearsService.getArrearsDetailList(regionId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("arrearsDetailList", result);
        return R.data(resultMap);
    }
    
    /**
     * 计算公司总欠款
     */
    @GetMapping("/total")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算公司总欠款", notes = "计算公司总欠款金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculateTotalArrears(
            @ApiParam(value = "代办人ID", required = false) @RequestParam(required = false) Long agentId,
            @ApiParam(value = "客户ID", required = false) @RequestParam(required = false) Long customerId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal totalArrears = arrearsService.calculateTotalArrears(agentId, customerId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("totalArrears", totalArrears);
        return R.data(result);
    }
    
    /**
     * 计算代办欠款
     */
    @GetMapping("/agent/single")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算代办欠款", notes = "计算指定代办人的欠款金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculateAgentArrears(
            @ApiParam(value = "代办人ID", required = false) @RequestParam(required = false) Long agentId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal agentArrears = arrearsService.calculateAgentArrears(agentId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("agentArrears", agentArrears);
        return R.data(result);
    }
    
    /**
     * 计算采购欠款
     */
    @GetMapping("/purchase/single")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算采购欠款", notes = "计算指定客户的采购欠款金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculatePurchaseArrears(
            @ApiParam(value = "客户ID", required = false) @RequestParam(required = false) Long customerId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal purchaseArrears = arrearsService.calculatePurchaseArrears(customerId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("purchaseArrears", purchaseArrears);
        return R.data(result);
    }
    
    /**
     * 计算代办未对账金额
     */
    @GetMapping("/agent/unreconciled")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算代办未对账金额", notes = "计算指定代办人的未对账金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculateAgentUnreconciled(
            @ApiParam(value = "代办人ID", required = false) @RequestParam(required = false) Long agentId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal agentUnreconciled = arrearsService.calculateAgentUnreconciled(agentId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("agentUnreconciled", agentUnreconciled);
        return R.data(result);
    }
    
    /**
     * 计算代办已对账未付款金额
     */
    @GetMapping("/agent/unpaid")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算代办已对账未付款金额", notes = "计算指定代办人的已对账未付款金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculateAgentUnpaid(
            @ApiParam(value = "代办人ID", required = false) @RequestParam(required = false) Long agentId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal agentUnpaid = arrearsService.calculateAgentUnpaid(agentId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("agentUnpaid", agentUnpaid);
        return R.data(result);
    }
    
    /**
     * 计算采购未对账金额
     */
    @GetMapping("/purchase/unreconciled")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算采购未对账金额", notes = "计算指定客户的采购未对账金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculatePurchaseUnreconciled(
            @ApiParam(value = "客户ID", required = false) @RequestParam(required = false) Long customerId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal purchaseUnreconciled = arrearsService.calculatePurchaseUnreconciled(customerId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("purchaseUnreconciled", purchaseUnreconciled);
        return R.data(result);
    }
    
    /**
     * 计算采购已对账未付款金额
     */
    @GetMapping("/purchase/unpaid")
    // @RequiresPermissions("finance:arrears:list")
    @ApiOperation(value = "计算采购已对账未付款金额", notes = "计算指定客户的采购已对账未付款金额")
    @ApiResponses({
        @ApiResponse(code = 0, message = "请求成功"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public R calculatePurchaseUnpaid(
            @ApiParam(value = "客户ID", required = false) @RequestParam(required = false) Long customerId,
            @ApiParam(value = "地域标志: 0-寿光, 1-河北, 2-聊城, 3-云南", required = false) @RequestParam(required = false) Integer regionId) {
        
        BigDecimal purchaseUnpaid = arrearsService.calculatePurchaseUnpaid(customerId, regionId);
        Map<String, Object> result = new HashMap<>();
        result.put("purchaseUnpaid", purchaseUnpaid);
        return R.data(result);
    }
}